#include<iostream>
#include<vector>
#include<cassert>
using std::vector;
using std::endl;
using std::cout;
using std::string;

class Point{
public:
    Point(){}
    Point(int x,int y)
    :_x(x)
    ,_y(y)
    {}
    void print(){
        cout<<"Point "<<_x<<" "<<_y<<endl;
    }
    ~Point(){
        cout<<"~Point"<<endl;
    }
    void Init(int x,int y){
        _x = x;
        _y = y;
    }

private:
    int _x;
    int _y;

};

class Computer{
public:
    Computer(){cout<<"Computer()"<<endl;}
    Computer(const string&name,const int& price)
    :_name(name)
    ,_price(price){cout<<"Computer(const string&name,const int& price)"<<endl;}
    ~Computer(){
        cout<<"~Computer"<<endl;
    }
    
    void print(){
        cout<<"name: "<<_name<<endl;
        cout<<"price: "<<_price<<endl;
    }
    void Init(const string&name,const int& price){
        _name = name;
        _price = price;
    }
private:
    string _name;
    int _price;
};

//rhs 要特例化
template<class T>
class Singleton{
public:
    
    template<class ...Args>
    static T*getInstance(){
        if(_single == nullptr){
            _single = new T();
        }
        //Point*
        return _single;
    }

    static void destory(){
        if(_single!=nullptr){
            cout<<"~destory"<<endl;
            delete _single;
            _single = nullptr;
        }
    }
private:
    //这里我思索的是，因为具体的类不需要在单例模式里面去构造
    //而是具体的类型里面就完成了构造
    static T* _single; //static point* _single;
};
//...
template<class T>
T *Singleton<T>::_single = nullptr;

void test0(){
    Point *pt3 = Singleton<Point>::getInstance();
    pt3->Init(4,6);
    pt3->print();
    Point *pt4 = Singleton<Point>::getInstance();
    pt4->Init(6,5);
    //检测地址

   
    printf("%p\n",pt3);
    printf("%p\n",pt4);
    //Singleton<Point>::getInstance(1,3);
    //Singleton<Point>::destory();
    //Singleton<Point>::destory();
    pt4->print();
}
void test1(){

    Computer *cp1 = Singleton<Computer>::getInstance();
    cp1->Init("huawei",8888);
    cp1->print();
    Computer *cp2 = Singleton<Computer>::getInstance();
    cp2->Init("xiaomi",4500);
    cp2->print();
    printf("%p \n%p \n",cp1,cp2);
    Singleton<Computer>::destory();
}

int main(void){
    test1();
}